<?php

namespace Lib\Utils;

class Funciones {
	
	/** Convierte un objeto stdclass a una array **/
	function objectToArray($d) {
		if (is_object ( $d )) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars ( $d );
		}
		
		if (is_array ( $d )) {
			/*
			 * Return array converted to object Using __FUNCTION__ (Magic constant) for recursive call
			 */
			return array_map ( __FUNCTION__, $d );
		} else {
			// Return array
			return $d;
		}
	}
	
	
	/** Convierte una array multidimensional en un objeto **/
	function arrayToObject($d) {
		if (is_array($d)) {
			/*
				* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return (object) array_map(__FUNCTION__, $d);
		}
		else {
			// Return object
			return $d;
		}
	}
	
	static function getPost(){
		$aux = new \stdClass();
		foreach ($_POST as $param_name => $param_val) {
			$aux->$param_name = $param_val;
			//echo "Param: $param_name; Value: $param_val<br />\n";
		}
		return $aux;
	}
	
	static function splitString($aux,$array){
		return explode($aux,$array);
	}
	
	/*
	 * 
	 * 
	 */
	
	static function compilar(){
		$php = "D:\\xampp\php\php.exe ";
		$composer = "D:\\xampp\htdocs\admin_fcf\composer.phar ";
		$cmd = 'dump-autoload';
		try {
			exec($php.$composer.$cmd,$output, $return_var);
			$result['success'] = 'OK';
			$result['output'] = $output;
			return $result;
		
		}catch(Exception $e) {
			return $e->getMessage();
		}
	}
	
		
	static function raw_json_encode($input) {
	
		return preg_replace_callback(
				'/\\\\u([0-9a-zA-Z]{4})/',
				function ($matches) {
					return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
				},
				json_encode($input)
		);
	
	}
	
	//longitud,mayusculas(true),numeros(true),caracteres especiales(false)
	static function randomString($length=10,$uc=TRUE,$n=TRUE,$sc=FALSE){

			$source = 'abcdefghijklmnopqrstuvwxyz';
			if($uc==1) $source .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			if($n==1) $source .= '1234567890';
			if($sc==1) $source .= '|@#~$%()=^*+[]{}-_';
			if($length>0){
				$rstr = "";
				$source = str_split($source,1);
				for($i=1; $i<=$length; $i++){
					mt_srand((double)microtime() * 1000000);
					$num = mt_rand(1,count($source));
					$rstr .= $source[$num-1];
				}
			}
			return $rstr;
	}
	
	static function stringMin($string){
		return strtolower($string);
	}
	
	static function replaceChar($string,$char,$replace){
		return str_replace($char,$replace,$string);
	}
	
	
}

?>